struct point
{
	double x, y;
    point() {};
	point(double x, double y): x(x), y(y) {};
};

double sqr(double x)
{
    return x * x;
}

double rast(point & a, point & b)
{
    return sqr(a.x - b.x) + sqr(a.y - b.y);
}

struct segment
{
    point a, b;
    segment() {};
    segment(point a, point b): a(a), b(b) {};
    double length()
    {
        return rast(a, b);
    }
};

double dist(point & p, segment & s)
{
        double d = sqr(s.b.x * s.a.y - s.a.x * s.b.y + p.x * (s.b.y - s.a.y) + p.y * (s.a.x - s.b.x)) / s.length();

        double min_dist = rast(p, s.b);
        double max_dist = rast(p, s.a);
        
        if (max_dist < min_dist)
             std::swap(min_dist, max_dist);

        return max_dist - d > s.length() ? min_dist : d;
}
